package com.zhangliang.oa.crud;

import com.zhangliang.oa.bean.Dept;
import com.zhangliang.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@WebServlet({"/dept/list", "/dept/add", "/dept/del", "/dept/select", "/dept/update"})
public class DeptServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        if ("/dept/list".equals(servletPath)) {
            doList(request, response);
        }
        if ("/dept/add".equals(servletPath)) {
            doAdd(request, response);
        }
        if ("/dept/del".equals(servletPath)) {
            doDel(request, response);
        }
        if ("/dept/select".equals(servletPath)) {
            doSelect(request, response);
        }
        if ("/dept/update".equals(servletPath)) {
            doUpdate(request, response);
        }
    }

    private void doUpdate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String contextPath = request.getContextPath();
        String deptno = request.getParameter("deptno");
        String dname = request.getParameter("dname");
        String loc = request.getParameter("loc");
        Connection conn = null;
        PreparedStatement ps = null;
        int i = 0;
        try {
            conn = DBUtil.getConnection();
            String sql = "update dept set dname = ?,loc = ? where deptno = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, dname);
            ps.setString(2, loc);
            ps.setString(3, deptno);
            i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, null);
        }
        if (i == 1) {
            response.sendRedirect(contextPath + "/dept/list");
        } else {
            response.sendRedirect(contextPath + "/error.jsp");
        }
    }

    private void doSelect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Dept dept = new Dept();
        String deptno = request.getParameter("deptno");
        try {
            conn = DBUtil.getConnection();
            String sql = "select dname,loc from dept where deptno = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, deptno);
            rs = ps.executeQuery();
            while (rs.next()) {
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");
                dept.setDeptno(deptno);
                dept.setDname(dname);
                dept.setLoc(loc);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        request.setAttribute("select", dept);
        String b = request.getParameter("b");
        if ("c".equals(b)) {
            request.getRequestDispatcher("/select.jsp").forward(request, response);
        } else if ("x".equals(b)) {
            request.getRequestDispatcher("/edit.jsp").forward(request, response);
        }
    }

    private void doDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String deptno = request.getParameter("deptno");
        Connection conn = null;
        PreparedStatement ps = null;
        String contextPath = request.getContextPath();
        int i = 0;
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement("delete from dept where deptno = ?");
            ps.setString(1, deptno);
            i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, null);
        }
        if (i == 1) {
            response.sendRedirect(contextPath + "/dept/list");
        } else {
            response.sendRedirect(contextPath + "/error.jsp");
        }
    }

    private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String deptno = request.getParameter("deptno");
        String dname = request.getParameter("dname");
        String loc = request.getParameter("loc");
        Connection conn = null;
        PreparedStatement ps = null;
        String contextPath = request.getContextPath();
        int i = 0;
        try {
            conn = DBUtil.getConnection();
            ps = conn.prepareStatement("insert into dept(deptno,dname,loc) values (?,?,?)");
            ps.setString(1, deptno);
            ps.setString(2, dname);
            ps.setString(3, loc);
            i = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, null);
        }
        if (i == 1) {
            response.sendRedirect(contextPath + "/dept/list");
        } else {
            response.sendRedirect(contextPath + "/error.jsp");
        }
    }

    private void doList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Dept> list = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            String sql = "select deptno,dname,loc from dept";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                String deptno = rs.getString("deptno");
                String dname = rs.getString("dname");
                String loc = rs.getString("loc");
                Dept depts = new Dept();
                depts.setDeptno(deptno);
                depts.setDname(dname);
                depts.setLoc(loc);
                list.add(depts);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, ps, rs);
        }
        request.setAttribute("dept", list);
        request.getRequestDispatcher("/list.jsp").forward(request, response);
    }
}
